package com.example.jacky.uilibrary.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.jacky.uilibrary.R;


public class TabIndicator extends RelativeLayout {

    private ImageView mIvTabIcon;
    private TextView mTvTabHint;
    private TextView mTvTabUnRead;
    private Context mContext;

    private int mFocusId = -1, mNormalId = -1;
    private int mUnreadCount = 0;
    private int mImgResId = -1;
    public TabIndicator(Context context) {
        this(context, null);
    }

    public TabIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        View.inflate(context, R.layout.tab_item_view, this);
        mIvTabIcon = (ImageView) findViewById(R.id.tab_img_title);
        mTvTabHint = (TextView) findViewById(R.id.tab_tv_title);
        mTvTabUnRead = (TextView) findViewById(R.id.tab_unread);
        setUnread(0);
    }

    public void setTabIcon(int normalId, int focusId) {
        this.mNormalId = normalId;
        this.mFocusId = focusId;
        mIvTabIcon.setBackgroundResource(mNormalId);
    }

    public void setTabHint(int hintId) {
        mTvTabHint.setText(hintId);
    }

    public void setTabHint(String hintStr) {
        mTvTabHint.setText(hintStr);
    }

    public void setUnread(int unreadCount) {
        this.mUnreadCount = unreadCount;

        if (unreadCount <= 0) {
            mTvTabUnRead.setVisibility(View.GONE);
        } else {
            if (unreadCount >= 100) {
                mTvTabUnRead.setText("99+");
            } else {
                mTvTabUnRead.setText("" + unreadCount);
            }
            mTvTabUnRead.setVisibility(View.VISIBLE);
        }
    }

    public int getUnreadCount() {
        return this.mUnreadCount;
    }

    public void setCurrentFocus(boolean isCurrent) {
        if (isCurrent) {
            if (mFocusId != -1) {
                mIvTabIcon.setImageResource(mFocusId);
                mTvTabHint.setTextColor(mContext.getResources().getColor(R.color.green));
            }
        } else {
            if (mNormalId != -1) {
                mIvTabIcon.setImageResource(mNormalId);
                mTvTabHint.setTextColor(mContext.getResources().getColor(R.color.bg_gray));
            }
        }
    }

}
